<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiBaseController;
use App\Components\Tools;
//加载七牛
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
use Qiniu\Processing\PersistentFop;

class UploadController extends ApiBaseController{
	private $ak = '';       //jRIhIKAAwTr6ufz9s7jRmSB0jY0bmEGSxveMu4Bb
	private $sk = '';		//-1ZkUnYaGlPvmruF5VDCyNmzgtFmCLquBkcFkNJ0
	private $bucketName = '';	//yks-dy
	private $pipeline = 'yks_wlys';  //转码时使用的队列名称
	// private $notifyUrl = 'http://callback.weilaiyinsu.com/qiniu/notify';
	private $img_size = 2048000; //2M     102400   100M
	private $video_size = 51200000; // 131072000	128M  	 51200000   50M  
	private $img_type_arr = ['image/gif','image/pjpeg','image/jpeg','image/png', 'image/x-png','image/bmp'];
	private $video_type_arr = ['video/mp4','video/webm'];
	private $imgPath = "";
	private $videoPath = "";
	private $url = "";
	
	function __construct(Request $request){
        $this->parames = $request->all();
		$this->imgPath="data/dy/img/".($this->imgPath).date('Y-m-d',time())."/";
		$this->videoPath="data/dy/video/".($this->videoPath).date('Y-m-d',time())."/";
		$this->ak=Tools::getOneconfig("/config/third.json",array('qiniu','qnAccesskey'));
		$this->sk=Tools::getOneconfig("/config/third.json",array('qiniu','qnSecretkey'));
		$this->bucketName=Tools::getOneconfig("/config/third.json",array('qiniu','qnBucket'));
		$this->url=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
		
    }
	
	function qiniuUpload(){
		// echo "<pre/>";print_r($_FILES);exit;
		set_time_limit(0);
		$fileImgArr=isset($_FILES['fileImg']['name']) ? $_FILES['fileImg'] : '';
		$fileVideoArr=isset($_FILES['fileVideo']['name']) ? $_FILES['fileVideo'] : '';
		
		$data=array('imgUrl'=>'','videoUrl'=>'');
		if($fileImgArr){
			$data['imgUrl']=$this->uploadImg($fileImgArr);
		}
		if($fileVideoArr){
			$data['videoUrl']=$this->uploadVideo($fileVideoArr);
		}
		$this->returnJson(0,'上传成功','',$data);
	}
	
	//上传图片
	function uploadImg($imgData){
		$oldImgPath=isset($imgData['tmp_name']) && !empty($imgData['tmp_name']) ? $imgData['tmp_name'] : $this->returnJson(1,'图片不存在');
		$imgType=isset($imgData['type']) && !empty($imgData['type']) ? $imgData['type'] : $this->returnJson(1,'图片不存在');
		$imgSize=isset($imgData['size']) && !empty($imgData['size']) ? $imgData['size'] : $this->returnJson(1,'图片不存在');
		if(!in_array($imgType,$this->img_type_arr)){
			$this->returnJson(1,'图片格式错误');
		}
		if($imgSize > $this->img_size){
			$this->returnJson(1,'请上传小于2M的图片');
		}
		$typeName=explode('/',$imgType);
		$typeName=end($typeName);
		$imgName=($this->imgPath).(md5(time()).".".$typeName);
		$imgContent=file_get_contents($oldImgPath);
		$auth=new Auth($this->ak,$this->sk);
		$token=$auth->uploadToken($this->bucketName);
		$uploadManager = new UploadManager();
		list($re,$error)=$uploadManager->put($token,$imgName,$imgContent);
		if(empty($error)){
			return ($this->url)."/".$imgName;
		}
		return false;
	}
	
	//上传视频
	function uploadVideo($videoData){
		$oldVideoPath=isset($videoData['tmp_name']) && !empty($videoData['tmp_name']) ? $videoData['tmp_name'] : $this->returnJson(1,'视频不存在');
		$videoType=isset($videoData['type']) && !empty($videoData['type']) ? $videoData['type'] : $this->returnJson(1,'视频不存在');
		$videoSize=isset($videoData['size']) && !empty($videoData['size']) ? $videoData['size'] : $this->returnJson(1,'视频不存在');
		if(!in_array($videoType,$this->video_type_arr)){
			$this->returnJson(1,'只支持mp4、webm');
		}
		if($videoSize>$this->video_size){
			$this->returnJson(1,'请上传小于50M的视频');
		}
		$typeName=explode('/',$videoType);
		$typeName=end($typeName);
		$videoName=($this->videoPath).(md5(time()).".".$typeName);
		$auth=new Auth($this->ak,$this->sk);
		$policy=array(
			'persistentPipeline'=>$this->pipeline
		);
		$token=$auth->uploadToken($this->bucketName,null,86400,$policy);
		$uploadManager = new UploadManager();
		list($re,$error)=$uploadManager->putFile($token,$videoName,$oldVideoPath);
		if(empty($error)){
			return ($this->url)."/".$videoName;
		}
		return false;
	}
	
}